﻿using AgencyManagerSystem.Common;
using AgencyManagerSystem.Core.Utility;
using AgencyManagerSystem.DataFetcher;
using AgencyManagerSystem.Model;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;

namespace AgencyManagerSystem
{
    [EnableCors("any")]
    [Route("api/[controller]/[action]")]
    [ApiController]
    public abstract class MyControllerBase: ControllerBase
    {
        protected UserProfile profile;
        protected string access_token = "";
        public MyControllerBase()
        {
            MyHttpContextHelper.SetHttpContext(out access_token);
            profile = MyHttpContext.Current.Items[Definer.CURRENT_PROFILE] as FullUserProfile;
        }

        ~MyControllerBase()
        {
            if (DBHelper.Alive())
            {
                LogHelper.Log(this.GetType(), String.Format("回滚已经失效的事务"));
                DBHelper.Commit( false);
            }
        }
        
        protected string RequestIp
        {
            get { return Request.HttpContext.Connection.RemoteIpAddress.ToString(); }
        }
    }
}
